package com.demo.zhou.sniper.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


import com.demo.zhou.sniper.R;
import com.demo.zhou.sniper.adapter.ZuArrayAdapter;
import com.demo.zhou.sniper.application.MyApplication;
import com.demo.zhou.sniper.gen.DaoSession;
import com.demo.zhou.sniper.gen.QunbeanDao;
import com.demo.zhou.sniper.gen.ZubeanDao;
import com.demo.zhou.sniper.module.Zubean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhou on 2017/7/25.
 */

public class ZudetailsActivity extends BaseActivity {
    private ListView zulist_lv_list;
    private List<Zubean> zuAll = new ArrayList<>();
    private List<String> zuList = new ArrayList<>();
    DaoSession daoSession;
    QunbeanDao qunbeanDao;
    ZubeanDao zubeanDao;
    ArrayAdapter<String> adapter;
    private Long qunId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("组详情");
        Intent intent = getIntent();
        qunId = intent.getLongExtra("qunId",0);
        zulist_lv_list = (ListView) findViewById(R.id.zulist_lv_list);
        daoSession = ((MyApplication) getApplication()).getDaoSession();
        qunbeanDao = daoSession.getQunbeanDao();
        zubeanDao = daoSession.getZubeanDao();
        zuList = queryZu(qunId);
        adapter = new ZuArrayAdapter(this,
                R.layout.item_allqun_list,
                R.id.tv_item,
                zuList);
        zulist_lv_list.setAdapter(adapter);
        onClick();
    }
    @Override
    protected void onClickLeft() {
        super.onClickLeft();

    }
    public void onClick() {
        // 添加长按点击,得到点中的index，即参数arg2
        zulist_lv_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                           final int arg2, long arg3) {
                //定义AlertDialog.Builder对象，当长按列表项的时候弹出确认删除对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(ZudetailsActivity.this);
                builder.setMessage("确定删除?");
                builder.setTitle("提示");

                //添加AlertDialog.Builder对象的setPositiveButton()方法
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteData(zuAll.get(arg2).getId());
                        if (zuList.remove(arg2) != null) {
                            System.out.println("success");
                        } else {
                            System.out.println("failed");
                        }
                        adapter.notifyDataSetChanged();
                        Toast.makeText(ZudetailsActivity.this, "删除成功！", Toast.LENGTH_SHORT).show();
                    }
                });

                //添加AlertDialog.Builder对象的setNegativeButton()方法
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                builder.create().show();
                return true;
            }
        });
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_zhulist;
    }

    private List<String> queryZu(Long qunId) {
        List<String> zuDate = new ArrayList<>();
        zuAll = qunbeanDao.load(qunId).getZu();
        for(Zubean zb: zuAll) {
            if (zb.getMos()){
                zuDate.add(zb.getXian() + "s" + zb.getFanganhao());
            }else {
                zuDate.add(zb.getXian() + "y" + zb.getFanganhao());
            }
        }

        return zuDate;
    }

    public void deleteData(Long index) {
        zubeanDao.deleteByKey(index);
        qunbeanDao.load(qunId).resetZu();
    }
}
